//
//  LPTAccountWaterViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/6.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTAccountWaterViewController.h"
#import "EarnTableViewCell.h"
#import "LPTIncomeDetailTableViewController.h"

@interface LPTAccountWaterViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (assign, nonatomic)NSInteger page;
@property (strong, nonatomic)NSIndexPath *indexPath;

@end

@implementation LPTAccountWaterViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.page = 1;
    
    [self initSubViews];
    
    if (self.type == kAccountWaterType) {
        
        self.title = @"账户流水";
        [self getAccountLogList];
    }else{
        
        self.title = @"提现记录";
        [self getTakeOutList];
    }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushToIncomeDetail"]) {
        
        //跳转到收益详情
        LPTIncomeDetailTableViewController *vc = segue.destinationViewController;
        
        NSDictionary *dic = self.dataSource[self.indexPath.row];
        
        if (self.type == kAccountWaterType) {
            
            //账户流水
            vc.dic = dic;
            vc.type = kZhanghuliushuiType;
            
        }else{
            
            //提现记录
            vc.dic = dic;
            vc.type = kTixianjiluType;
        }
    }
}

-(void)initSubViews
{
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 67;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if (self.type == kAccountWaterType) {
            
            [self getAccountLogList];
        }else{
            
            [self getTakeOutList];
        }
    }];
    
    self.tableView.mj_header = header;
    
}

//获取流水记录
-(void)getAccountLogList
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/account_log_list" withBody:@{@"page":@(self.page),@"size":@(1000)}];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
       
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"account_logs"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

//获取提现记录
-(void)getTakeOutList
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/cash_list" withBody:@{@"page":@(self.page),@"size":@(1000)}];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"cashes"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

#pragma mark - UITableViewDataSource & Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EarnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EarnTableViewCell" forIndexPath:indexPath];
    [cell refreshWithDic:self.dataSource[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.indexPath = indexPath;
    [self performSegueWithIdentifier:@"pushToIncomeDetail" sender:nil];
}

@end
